clear;clc;

load Data_CRSPMonthly N T permno yyyymm SzCat Excd Price Volat; % in the monthly file, these data are NOT lagged
NASD = Excd==3;
Micro = SzCat==3;
Small = SzCat==2;
Large = SzCat==1;
LnPrice = log(Price); LnPrice(~isfinite(LnPrice)) = NaN;
clear SzCat Excd Price;

CoeffAll = [
    1.618   1.266  -0.018  -0.080  0.061  0.123    2.030  2.368  2.796  0.276  0.223  -0.024;
    4.826   0.908   0.275   1.973  0.506  0.509    2.576  1.134  0.964  2.044  0.203   0.031;
    17.950  1.213   0.120   4.684  9.332  5.073  -10.200  1.397  1.791  4.340  0.045  -0.167];
CoeffAll = CoeffAll/100;

Coeffs = CoeffAll(1,:);
X = Coeffs(1)*Micro + Coeffs(2)*Small + Coeffs(3)*Large + Coeffs(4)*NASD.*Micro + Coeffs(5)*NASD.*Small + Coeffs(6)*NASD.*Large + Coeffs(7)*Volat.*Micro + Coeffs(8)*Volat.*Small + Coeffs(9)*Volat.*Large -Coeffs(10)*LnPrice.*Micro - Coeffs(11)*LnPrice.*Small - Coeffs(12)*LnPrice.*Large;
Lambda_Continuous = [NaN(N,1) X(:,1:end-1)];

Coeffs = CoeffAll(2,:);
X = Coeffs(1)*Micro + Coeffs(2)*Small + Coeffs(3)*Large + Coeffs(4)*NASD.*Micro + Coeffs(5)*NASD.*Small + Coeffs(6)*NASD.*Large + Coeffs(7)*Volat.*Micro + Coeffs(8)*Volat.*Small + Coeffs(9)*Volat.*Large -Coeffs(10)*LnPrice.*Micro - Coeffs(11)*LnPrice.*Small - Coeffs(12)*LnPrice.*Large;
Lambda_Closing = [NaN(N,1) X(:,1:end-1)];

Coeffs = CoeffAll(3,:);
X = Coeffs(1)*Micro + Coeffs(2)*Small + Coeffs(3)*Large + Coeffs(4)*NASD.*Micro + Coeffs(5)*NASD.*Small + Coeffs(6)*NASD.*Large + Coeffs(7)*Volat.*Micro + Coeffs(8)*Volat.*Small + Coeffs(9)*Volat.*Large -Coeffs(10)*LnPrice.*Micro - Coeffs(11)*LnPrice.*Small - Coeffs(12)*LnPrice.*Large;
Lambda_Opening = [NaN(N,1) X(:,1:end-1)];

save Data_Lambdas Lambda* -v7.3